package com.dominator.utils.dateutil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 获取两个月之间所有月份的集合
     *
     * @param minDate
     * @param maxDate
     * @return
     */
    synchronized public static List<String> getMonthBetween(String minDate, String maxDate) {
        ArrayList<String> result = new ArrayList<String>();


        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        try {
            min.setTime(StringToDateFormat(minDate, "yyyy-MM"));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(StringToDateFormat(maxDate, "yyyy-MM"));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("DateUtil  have some error" + e);
        }

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(DatetoStringFormat(curr.getTime(), "yyyy-MM"));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    /**
     * 日期类型转换为string类型
     *
     * @return
     */
    synchronized public static String DatetoString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String str = sdf.format(date);
        return str;
    }

    /**
     * 根据给定的日期格式转换日期
     *
     * @param date
     * @param format
     * @return
     */

    synchronized public static String DatetoStringFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 根据给定的格式字符串转换成date
     *
     * @param str
     * @return
     */
    synchronized public static Date StringToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据给定的格式字符串转换成date
     *
     * @param str
     * @return
     */
    synchronized public static Date StringToDateFormat(String str, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(str);
        return date;
    }

    /**
     * 获取传入日期的前n个月
     *
     * @param date
     * @param n
     * @return
     */
    synchronized public static Date minusMonth(Date date, int n) {
        logger.info("n:{}", n);
        logger.info("date:{}", DatetoStringFormat(date, "yyyy-MM-dd"));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - n);
        Date date1 = calendar.getTime();
        logger.info("date1:{}", DatetoStringFormat(date1, "yyyy-MM-dd"));
        return date1;
    }

//    public static void main(String[] args) {
//        Date date1 = new Date();
//        String str = DatetoStringFormat(date1, "yyyy-MM-dd HH:mm");
//        logger.info("str:{}", str);
////        String str = "2017-11-24 16:00:31";
//
//    }

}
